import { Component } from "@angular/core";
import { NavController, NavParams } from "ionic-angular";
import { HttpService } from "./../../providers/http.service";
import { CommonFunc } from "./../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { InformDetail } from "./../../pages/refresher/inform-detail/inform-detail";
import { NoticeDetail } from "./../../pages/refresher/notice-detail/notice-detail";
import { SignPage } from "../sign/sign";
import { ApplyRepairDetailPage } from "../repair-apply-detail/apply-repair-detail";
import { SafeControlDetailPage } from "../supervision/safe-control-detail/safe-control-detail";
import {RepairDetailPage} from "../used-repair/repair-detail/repair-detail";
import {SafeControlDownload} from "../supervision/safe-control/safe-control-download/safe-control-download";

@Component({
  templateUrl: "refresher.html"
})
export class Refresher {
  private title: string; //标题
  private list: any = [];
  private pageSize: number = 10;
  private currentPage: number = 1;
  private currentUserId: any;
  private partyId: any;
  private parentId: any;
  isNotice: boolean = false;
  isRead: any = "0";
  refresher: any;
  private isShowNoData = false;
  private totalPage = 1; // 共多少页
  constructor(
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private httpService: HttpService,
    private storage: Storage,
    private navController: NavController
  ) {
    if (navParams.get("param") == "1") {
      this.isNotice = true;
    }
  }
  ionViewWillEnter() {
    //  当前页小于大于总页数时，将总页数赋值给当前值
    if(this.totalPage<this.currentPage){
      this.currentPage = this.totalPage;
    }else {
      this.query();
    }
  }

  selected() {
    if (this.refresher) {
      this.refresher.enabled = true;
      this.isShowNoData = false;
    }
    this.pageSize = 10;
    this.currentPage = 1;
    this.query();
  }
  query() {
    if (this.navParams.get("param") == "1") {
      this.list = [];
      this.title = "待办及提醒";
      this.storage.get("user").then(data => {
        this.currentUserId = data.id;
        let param = {
          pageSize: this.pageSize,
          currentPage: this.currentPage,
          currentUserId: data.id,
          isRead: this.isRead
        };
        this.httpService.noticeQueryList(param).subscribe(data => {
          if (data.status != 200) {
            let toast = this.commonFunc.showInsideErrorToast(data);
            toast.present();
            return;
          }
          let ret = Object.assign({}, data.data.list);
          if (ret) {
            for (let i in ret) {
              if (ret[i]["title"] && (
                ret[i]["title"].includes("定检超期") ||
                ret[i]["title"].includes("维保完成") ||
                ret[i]["title"].includes("电梯维保") ||
                ret[i]["title"].includes("电梯年检") ||
                ret[i]["title"].includes("电梯年检"))
              ) {
                ret[i]["imgUrl"] = "assets/icon/icon_remind@2x.png";
              } else {
                // 默认采用通知类型
                ret[i]["imgUrl"] = "assets/icon/icon_notice@2x.png";
              }
            }
          }
          for (let i in ret) {
            this.list.push(ret[i]);
          }
          this.totalPage = Math.ceil(data.data.total/10) ;
          this.currentPage = this.currentPage > this.totalPage ? this.totalPage :this.currentPage;
          // this.list = ret;
        });
      });
    } else {
      this.title = "资讯";
      this.storage.get("user").then(data => {
        this.partyId = data.partyId;
        this.parentId = data.partyParentId;
        let param = {
          pageSize: this.pageSize,
          currentPage: this.currentPage,
          createPartyId: data.partyId,
          parentId: data.partyParentId
        };
        this.httpService.informationQueryList(param).subscribe(data => {
          if (data.status != 200) {
            let toast = this.commonFunc.showInsideErrorToast(data);
            toast.present();
            return;
          }
          this.list = data.data.list;
          this.totalPage = Math.ceil(data.data.total/10) ;
          this.currentPage = this.currentPage > this.totalPage ? this.totalPage :this.currentPage;
        });
      });
    }
  }

  //刷新
  doRefresh(refresher) {
    console.log(refresher,"1232112")
    if (!this.refresher) {
      this.refresher = refresher;
    }
    setTimeout(() => {
      this.currentPage++;
      let params: any = {};
      params.pageSize = this.pageSize;
      params.currentPage = this.currentPage;
      params.currentUserId = this.currentUserId;
      params.isRead = this.isRead;
      params.createPartyId = this.partyId;
      params.parentId = this.parentId;
      if (this.navParams.get("param") == "1") {
        let ob = this.httpService.noticeQueryList(params);
        ob.subscribe(data => {
          if (data.status != 200) {
            let toast = this.commonFunc.showInsideErrorToast(data.message);
            toast.present();
            return;
          }
          if (data.data.list && data.data.list.length > 0) {
            if (data.data.list) {
              let ret = Object.assign({}, data.data.list);
              for (let i in ret) {
                if (ret[i]["title"] && (
                  ret[i]["title"].includes("电梯将维保通知") ||
                  ret[i]["title"].includes("维保完成") ||
                  ret[i]["title"].includes("电梯维保过期通知") ||
                  ret[i]["title"].includes("电梯将年检通知") ||
                  ret[i]["title"].includes("电梯年检超期通知"))
                ) {
                  ret[i]["imgUrl"] = "assets/icon/icon_remind@2x.png";
                } else {
                  // 默认采用通知类型
                  ret[i]["imgUrl"] = "assets/icon/icon_notice@2x.png";
                }
              }
              for (let i in ret) {
                this.list.push(ret[i]);
              }
              this.totalPage = Math.ceil(data.data.total/10) ;
              this.currentPage = this.currentPage > this.totalPage ? this.totalPage :this.currentPage;
            }
          } else {
            this.refresher.enabled = false;
            this.isShowNoData = true;
            // setTimeout(()=> {
            //     refresher.enabled = true;
            // },3000);
          }
        });
      } else {
        let ob = this.httpService.informationQueryList(params);
        ob.subscribe(data => {
          if (data.status != 200) {
            let toast = this.commonFunc.showInsideErrorToast(data.message);
            toast.present();
            return;
          }

          if (data.data.list && data.data.list.length > 0) {
            if (data.data.list) {
              data.data.list.forEach(e => {
                this.list.push(e);
              });
              this.totalPage = Math.ceil(data.data.total/10) ;
              this.currentPage = this.currentPage > this.totalPage ? this.totalPage :this.currentPage;
            }
          } else {
            this.refresher.enabled = false;
            this.isShowNoData = true;
            // setTimeout(()=> {
            //     refresher.enabled = true;
            // },3000);
          }
        });
      }

      this.refresher.complete();
    }, 500);
  }

  detail(p) {
    console.log("p");
    console.log(p);
    let param = this.navParams.get("param");
    if (param == "1") {
      //判断是否是电梯维保的信息elevatoriMonId
      if (p.elevatoriType == "5") {
        let param = {
          id: p.elevatoriMonId,
          noticeId: p.id
        };
        if (this.isRead == "1") {
          param["dtype"] = "lk";
        }
        this.navController.push(ApplyRepairDetailPage, param);
        return;
      }
      if (p.elevatoriType == '6') { //监督检查
        this.navController.push(NoticeDetail, { id: p.id });
        // if(p.title == '监督检查'){
        //
        // }else{
        //   this.navController.push(SafeControlDownload, {
        //     safeConId: p.elevatoriMonId,
        //   });
        // }
        return;
      }

      if (p.elevatoriMonId != null && p.elevatoriMonId.length > 0 && p.elevatoriType != "8") {
        this.navController.push(SignPage, { ElevaType: "notice", params: p });
      } else if(p.elevatoriMonId != null && p.elevatoriType == "8" && p.isRead == "0"){
        this.navController.push(RepairDetailPage,{repairInfoId:p.elevatoriMonId, params:p,id: p.id});
      }else{
        this.navController.push(NoticeDetail, { id: p.id });
      }
    } else {
      this.navController.push(InformDetail, { id: p.id });
    }
  }
}
